<template>
  <div class='article-container'>
     <el-card>
        <el-row type="flex" justify="space-between">
            <el-col :span="18" :sm="18">
             <el-form size="small" >
              <el-row :gutter="10" >
                <el-col  :xs="24" :sm="24" :md="24" :lg="7">
                <el-form-item label-width="80px" label="关键字" style="width:270px">
                    <el-input  v-model="searchForm.keyword"></el-input>
                </el-form-item>
              </el-col>
               <el-col  :xs="24" :sm="24" :md="12" :lg="7">
                <el-form-item label-width="80px" label="状态" >
                     <el-select v-model="searchForm.state" placeholder="请选择">
                        <el-option
                          v-for="item in statusList"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                </el-form-item>
              </el-col>
              <el-col  :xs="24" :sm="24" :md="12" :lg="5">
                <el-form-item >
                    <el-row >
                      <el-button size="small" @click="clear">清除</el-button>
                      <el-button size="small" type="primary" @click="hSearch">搜索</el-button>
                    </el-row>
                </el-form-item>
              </el-col>
              </el-row>
            </el-form>
            </el-col>

            <el-col :span="6"  :sm="6">
              <el-row type="flex" justify="end">

                  <el-button type="success" size="small" @click="hAdd"><i class="el-icon-edit" /> 新增技巧</el-button>
              </el-row>
            </el-col>
          </el-row>
           <el-alert
            :title="alertText"
            type="info"
            class="alert"
            :closable="false"
            show-icon
            ></el-alert>
         <el-table
           :header-cell-style="{background:'#fafafa'}"
          :data="articleList"
          style="width: 100%"
          >
          <el-table-column
          type="index"
          label="序号"
          width="80">
        </el-table-column>
         <el-table-column

          label="文章标题"
          width="400">
          <template #default="{row}">
            {{row.title}}
            <i class="el-icon-film showVideo" v-if="row.videoURL" @click="showVideo(row.videoURL)"></i>
          </template>
        </el-table-column>
         <el-table-column
          prop="visits"
          label="阅读数"
          width="170">
        </el-table-column>
        <el-table-column
          prop="username"
          label="录入人"
          width="170">
        </el-table-column>
         <el-table-column
          label="录入时间"
          width="175">
          <template #default="{row}">
            {{formatDate(row.createTime)}}
          </template>
        </el-table-column>
         <el-table-column
          prop="twoLevelArticle"
          label="状态"
          width="100">
          <template #default="{row}">
            {{row.state?'已启用':'已禁用'}}
          </template>
        </el-table-column>
         <el-table-column
          label="操作"
         >
          <template #default="{row}">
            <el-button type="text" @click="hPreview(row)" >预览</el-button>
            <el-button type="text" @click="changState(row)" >{{row.state?'禁用':'启用'}}</el-button>
            <el-button type="text" @click="hEdit(row)" :disabled="row.state===1">修改</el-button>
            <el-button type="text" @click="hDel(row.id)" :disabled="row.state===1">删除</el-button>
          </template>
        </el-table-column>
          </el-table>
        <el-row type="flex" justify="end" class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            background
            @current-change="handleCurrentChange"
            :current-page="q.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="q.pagesize"
            layout="prev,pager,next,sizes,jumper"
            :total="total">
          </el-pagination>
      </el-row>
       <el-dialog
        @closed="hClose"
        :visible.sync="showDialog"
        width="800px"
        :title="title"
        :close-on-click-modal="false"
     >
      <articles-preview :curId="curId" :type="type" v-if="showDialog" @success='hSuccess' @close="hClose"></articles-preview>
     </el-dialog>
     </el-card>
     <div class="mask" v-if="previewVideo">
        <div class="close" @click="previewVideo=false"> <i class="el-icon-close"></i></div>
        <div class="videobox">
          <video :src="videoURL" controls class="video"></video>
        </div>
     </div>
  </div>
</template>

<script>
import moment from 'moment'
import * as articles from '@/api/hmmm/articles.js'
import { status } from '@/api/hmmm/constants.js'
import articlesPreview from '../components/articles-preview.vue'
export default {
  components: { articlesPreview },
  data () {
    return {
      statusList: [],
      alertText: '共9条数据',
      articleList: [],
      showDialog: false,
      type: 'add',
      total: 10,
      curId: '',
      searchForm: {
        keyword: '',
        state: ''
      },
      previewVideo: false,
      q: {
        page: 1,
        pagesize: 10
      },
      videoURL: ''
    }
  },

  created () {
    this.statusList = status
    this.loadArticleList()
  },
  methods: {
    showVideo (url) {
      this.videoURL = url
      this.previewVideo = true
    },
    hAdd () {
      this.curId = ''
      this.type = 'add'
      this.showDialog = true
    },
    hEdit (row) {
      this.curId = row.id
      this.type = 'edit'
      this.showDialog = true
    },
    hPreview (row) {
      this.curId = row.id
      this.type = 'view'
      this.showDialog = true
    },
    hDel (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await articles.remove({ id })
        this.loadArticleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hClose () {
      this.showDialog = false
    },
    hSuccess () {
      this.showDialog = false
      this.loadArticleList()
    },
    async changState (row) {
      if (row.state) {
        row.state = 0
      } else {
        row.state = 1
      }
      try {
        const res = await articles.changeState({ id: row.id, state: row.state })
        console.log(res)
        if (res.status === 200) {
          this.$message.success('操作成功')
          this.loadArticleList()
        }
      } catch (error) {
        console.log(error)
      }
    },
    async loadArticleList () {
      try {
        if (this.$route.query.id) {
          this.q.subjectID = this.$route.query.id
        }
        const res = await articles.list(this.q)
        if (res.status !== 200) return this.$message.error('获取失败')
        this.articleList = res.data.items
        this.total = res.data.counts
        this.alertText = `共${this.total}条数据`
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    clear () {
      this.searchForm = {
        keyword: '',
        state: ''
      }
      delete this.q.keyword
      delete this.q.state
    },
    async hSearch () {
      this.q = { ...this.q, ...this.searchForm }
      if (this.q.state === '') {
        delete this.q.state
      }
      if (this.q.keyword === '') {
        delete this.q.keyword
      }
      this.loadArticleList()
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.q.pagesize = val
      this.q.page = 1
      this.loadArticleList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.q.page = val
      this.loadArticleList()
    },
    formatDate (date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }

  },
  computed: {
    title () {
      if (this.type === 'add') {
        return '添加文章'
      } else if (this.type === 'edit') {
        return '修改文章'
      } else {
        return '预览文章'
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container{
   padding: 0 10px;
    margin: 10px 0;
     .alert {
  margin: 10px 0px;
  }
  .mask{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    overflow: hidden;
    z-index: 9999;
   .close{
  width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%);
    background: rgba(0,0,0,.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    z-index: 666;
    }
    .videobox{
      width: 800px;
      height: 600px;
      position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
     .video{
      width: 100%;
      height: 100%;
     }
    }
}
  }
  .showVideo{
    color: blue;
    font-size: 18px;
    cursor: pointer;
  }
  .pagination{
    margin: 20px 0;
  }

</style>
